﻿using System;
using System.Data;
using System.Data.DBLib;
using System.Web;
using SysComm;

namespace SysOperationLayer.DBHandle
{
    /// <summary>
    /// 对应于表dbf_log,此表为记录用户的操作的log
    /// 
    /// </summary>
    /// 
    public class LogDB : DBBase
    {
        public LogDB()
        {
        }

        public LogDB(CSqlDB objDB)
        {
            m_objDB = objDB;
        }

        /// <summary>
        /// 根据一定的条件获取日志的列表
        /// </summary>
        /// <param name="funcId"></param>
        /// <param name="fromTime"></param>
        /// <param name="toTime"></param>
        /// <returns></returns>
        public DataTable getLogList(string strFuncId, string strFromTime, string strToTime)
        {
            if (ConnectDB())
            {
                string strSql = "select l.*, Message + ErrorMessage as LogData, FunctionName from DBF_LOG l, DBF_FUNCTION f where l.FunctionID = f.FunctionID";

                if (!IsEmpty(strFuncId))
                {
                    strSql += " and l.FunctionID='" + strFuncId + "'";
                }

                if (!IsEmpty(strFromTime))
                {
                    strSql += " and LogTime >= '" + strFromTime + "'"; ;
                }

                if (!IsEmpty(strToTime))
                {
                    strSql += " and LogTime <= '" + strToTime + "'"; ;
                }

                strSql += " order by LogTime desc";

                return m_objDB.getDataTable(strSql, "DBF_LOG");
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return null;
        }

        /// <summary>
        /// 删除日志(以Rowno)
        /// </summary>
        /// <param name="Rowno">rowno</param>
        /// <returns></returns>
        public bool delLogByrowno(string strRowno)
        {
            if (ConnectDB())
            {
                CDBError objError = new CDBError();
                string strSql = "delete from DBF_LOG where Rowno = ";
                strSql += strRowno;
                m_objDB.updateData(strSql, objError);
                m_strMessage = objError.Message;
                
                return objError.RetCode == 0;
            }
            else
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return false;
        }

        /// <summary>
        /// 向数据库里写入日志
        /// </summary>
        /// <param name="funcId">模块的函数名</param>
        /// <param name="userId">当前用户ID</param>
        /// <param name="userType">用户组</param>
        /// <param name="message">消息</param>
        /// <param name="errorMessage">错误消息</param>
        /// <param name="status">当前的状态</param>
        /// <param name="updateIp">写入ip</param>
        /// <param name="updateTime">写入时间</param>
        /// <returns></returns>
        public int writeLog(string funcId,
                            string user,
                            string userType,
                            string message,
                            string errorMessage,
                            string status,
                            string updateIp,
                            string updateTime)
        {

            if (ConnectDB())
            {
                CDBError objError = new CDBError();
                string strSql = "insert into DBF_LOG(FunctionID, LogUser, UserType, Message, ErrorMessage, Status, LogIP, LogTime)";
                strSql += " values('";
                strSql += funcId;
                strSql += "','";
                strSql += user;
                strSql += "','";
                strSql += userType;
                strSql += "','";
                strSql += message;
                strSql += "','";
                strSql += errorMessage;
                strSql += "','";
                strSql += status;
                strSql += "','";
                strSql += updateIp;
                strSql += "',getDate()";
                strSql +=")";
                m_objDB.updateData(strSql, objError);
                m_strMessage = objError.Message;
                return objError.RetCode;
            }
            else 
            {
                m_strMessage = "Cannot Connect to the Database!";
            }

            return -1;
        }

        public int writeSucLog(string strFuncId, string strMessage)
        {
            return writeLog(strFuncId, 
                            HttpContext.Current.Session[SysUtil.m_str_session_username].ToString(),
                            HttpContext.Current.Session[SysUtil.m_str_session_usergroupname].ToString(),
                            strMessage, 
                            "", 
                            "SUCCEED", 
                            HttpContext.Current.Request.UserHostAddress, DateTime.Now.ToString());
        }

        public int writeFaileLog(string strFuncId, string strMessage)
        {
            return writeLog(strFuncId, 
                            HttpContext.Current.Session[SysUtil.m_str_session_username].ToString(),
                            HttpContext.Current.Session[SysUtil.m_str_session_usergroupname].ToString(),
                            strMessage, 
                            "", 
                            "FAILURE", 
                            HttpContext.Current.Request.UserHostAddress.ToString(), 
                            DateTime.Now.ToString());
        }

        public DataTable GetFunctionInfo()
        {
            if (ConnectDB())
            {
                string strSql = "select * from DBF_FUNCTION union select '', ''";
                return m_objDB.getDataTable(strSql, "DBF_FUNCTION");
            }

            return null;
        }
    }
}